package com.zk.iotgateway.netty.coder.workcard;

import com.zk.iotgateway.util.BytesUtils;
import com.zk.iotgateway.util.NettyUtil;
import com.zk.iotgateway.util.StringUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.util.ReferenceCountUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * 编码器 将对象 编码成字节数组  --->目的地是终端硬件
 *
 * @author:mn.m
 * @create 2019-04-19 下午 02:29
 **/
@Component
@Scope("prototype")
public class LocatorEncoder extends MessageToByteEncoder {

    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, Object o, ByteBuf byteBuf) throws Exception {
        String msg=(String) o;
        byte[] dist= StringUtils.decodeHex(msg);
        byteBuf.writeBytes(dist);
        ReferenceCountUtil.release(o);
    }
}
